{% from 'bootstrap/utils.html' import render_icon %}

{% macro deprecate_old_pk_placeholder() %}
    {{ warn('The default action primary key placeholder has changed to ":id", please update.
        The support to the old value (":primary_key") will be removed in version 2.0.') }}
{% endmacro %}


{% macro render_table(data,
                      titles=None,
                      primary_key='id',
                      primary_key_title='#',
                      caption=None,
                      table_classes=None,
                      header_classes=None,
                      responsive=False,
                      responsive_class='table-responsive',
                      show_actions=False,
                      actions_title='Actions',
                      custom_actions=None,
                      view_url=None,
                      edit_url=None,
                      delete_url=None,
                      new_url=None,
                      action_pk_placeholder=':id') %}
{% if not titles %}
    {% set titles = get_table_titles(data, primary_key, primary_key_title) %}
{% endif %}
{% if responsive %}
    <div class="{{ responsive_class }}">
{% endif %}
<table class="table{% if table_classes %}{{ ' ' + table_classes }}{% endif %}">
    {% if caption %}
    <caption>{{ caption }}</caption>
    {% endif %}
    <thead {% if header_classes %}class="{{ header_classes }}"{% endif %}>
    <tr>
    {% for title in titles %}
        <th scope="col">{{ title[1] }}</th>
    {% endfor %}
    {% if show_actions %}
        <th scope="col">{{ actions_title }}
        {% if new_url %}<a class="action-icon text-decoration-none" href="{{ new_url }}" title="{{ config['BOOTSTRAP_TABLE_NEW_TITLE'] }}">
            {{ render_icon('plus-circle-fill') }}
        </a>{% endif %}
        </th>
    {% endif %}
    </tr>
    </thead>
    <tbody>
    {% for row in data %}
    <tr>
        {% for title in titles %}
        {% if title[0] == primary_key %}
            <th scope="row">{{ row[title[0]] }}</th>
        {% else %}
            <td>{{ row[title[0]] }}</td>
        {% endif %}
        {% endfor %}
        {% if show_actions %}
        <td>
            {% if custom_actions %}
            {% for (action_name, action_icon, action_url) in custom_actions %}
                {% if ':primary_key' in action_url %}
                    {% set action_pk_placeholder = ':primary_key' %}
                    {% set w = deprecate_old_pk_placeholder() %}
                {% endif %}
                <a class="action-icon text-decoration-none"
                    href="{{ action_url | replace(action_pk_placeholder, row[primary_key]) }}" 
                    title="{{ action_name }}">{{ render_icon(action_icon) }}</a>
            {% endfor %}
            {% endif %}
            {% if view_url %}
                {% if ':primary_key' in view_url %}
                    {% set action_pk_placeholder = ':primary_key' %}
                    {% set w = deprecate_old_pk_placeholder() %}
                {% endif %}
            <a class="action-icon text-decoration-none"
                href="{{ view_url | replace(action_pk_placeholder, row[primary_key]) }}"
                title="{{ config['BOOTSTRAP_TABLE_VIEW_TITLE'] }}">
                {{ render_icon('eye-fill') }}
            </a>
            {% endif %}
            {% if edit_url %}
                {% if ':primary_key' in edit_url %}
                    {% set action_pk_placeholder = ':primary_key' %}
                    {% set w = deprecate_old_pk_placeholder() %}
                {% endif %}
            <a class="action-icon text-decoration-none"
                href="{{ edit_url | replace(action_pk_placeholder, row[primary_key]) }}"
                title="{{ config['BOOTSTRAP_TABLE_EDIT_TITLE'] }}">
                {{ render_icon('pencil-fill') }}
            </a>
            {% endif %}
            {% if delete_url %}
                {% if ':primary_key' in delete_url %}
                    {% set action_pk_placeholder = ':primary_key' %}
                    {% set w = deprecate_old_pk_placeholder() %}
                {% endif %}
            <form style="display:inline" action="{{ delete_url | replace(action_pk_placeholder, row[primary_key]) }}" method="post">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                <a class="action-icon text-decoration-none"
                    href="javascript:{}"
                    onclick="this.closest('form').submit();return false;"
                    title="{{ config['BOOTSTRAP_TABLE_DELETE_TITLE'] }}">
                    {{ render_icon('trash-fill') }}
                </a>
            </form>
            {% endif %}
        </td>
        {% endif %}
    </tr>
    {% endfor %}
    </tbody>
</table>
{% if responsive %}
    </div>
{% endif %}
{% endmacro %}
